package com.example.demo;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import org.tio.websocket.starter.EnableTioWebSocketServer;
import tk.mybatis.spring.annotation.MapperScan;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import com.example.demo.domain.Country;

@SpringBootApplication
/*
 * 在 starter 的逻辑中，如果你没有使用 @MapperScan 注解，
 * 你就需要在你的接口上增加 @Mapper 注解，否则 MyBatis 无法判断扫描哪些接口。
 * spring Boot 使用事务非常简单，首先使用注解 @EnableTransactionManagement开启事务支持后，然后在访问数据库的Service方法上添加注解 @Transactional 便可。
	@EnableTransactionManagement放在启动类上
 */
@MapperScan(basePackages = { "com.example.demo.dao" }, markerInterface = com.example.demo.common.BaseMapper.class)
/*

这里使用spring的@Mapperscan,会报找不到通用mapper中select方法的异常，暂不知道原因！！！！！！！！
 */
@EnableTransactionManagement
@Controller
//@EnableTioWebSocketServer
public class DemoApplication {

    @RequestMapping("/")
    public String index(Model model) {
        Country country = new Country();
        country.setCountryCode("EK");
        country.setCountryName("ElasKthod");
        List<Country> countrys = new ArrayList<>();
        Country country1 = new Country();
        country1.setCountryCode("GD");
        country1.setCountryName("GodOZZZ");
        Country country2 = new Country();
        country2.setCountryCode("SX");
        country2.setCountryName("SansXeno");
        Country country3 = new Country();
        country3.setCountryCode("DS");
        country3.setCountryName("DondSs");
        countrys.add(country1);
        countrys.add(country2);
        countrys.add(country3);
        //        model.addAttribute("country", country);
        //        model.addAttribute("countrys", countrys);
        return "index";
    }

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(DemoApplication.class);
        springApplication.run(args);
    }

    //    @Bean //必须new 一个RestTemplate并放入spring容器当中,否则启动时报错
   /* public RestTemplate restTemplate() {
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectionRequestTimeout(30 * 1000);
        httpRequestFactory.setConnectTimeout(30 * 3000);
        httpRequestFactory.setReadTimeout(30 * 3000);
        return new RestTemplate(httpRequestFactory);
    }*/

    @Bean
    public RestTemplate restTemplate() {
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectionRequestTimeout(20000);
        httpRequestFactory.setConnectTimeout(10000);
        httpRequestFactory.setReadTimeout(7200000);
        HttpClient httpClient = HttpClientBuilder.create().disableCookieManagement().disableRedirectHandling().build();
        httpRequestFactory.setHttpClient(httpClient);
        RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }

}
